<?php

namespace app\store\controller;

use app\store\model\Goods as GoodsModel;
use app\store\model\User as UserModel;
use app\store\model\user\Grade as GradeModel;
use app\store\service\Goods as GoodsService;
use think\Db;

/**
 * 用户管理
 * Class User
 * @package app\store\controller
 */
class Ceshi extends Controller
{
    /**
     * 用户列表
     * @param string $nickName 昵称
     * @param int $gender 性别
     * @param int $grade 会员等级
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index($nickName = '', $gender = null, $grade = null)
    {
        $model = new UserModel;
        $list = $model->getList($nickName, $gender, $grade);
        // 会员等级列表
        $gradeList = GradeModel::getUsableList();
        return $this->fetch('index', compact('list', 'gradeList'));
    }

    /**
     * 监控页
     */
    public function anslys(){

        return $this->fetch();
    }

    /**
     * 统计页
     */
    public function monitor(){

        return $this->fetch();
    }

    /**
     * 基础页
     */
    public function basecfg(){
        if (!$this->request->isAjax()) {
            $s_value = Db::name('configure')->where('s_type',1)->value('s_value');
            $config = unserialize($s_value);
            return $this->fetch('basecfg',compact('config'));
        }
        $param = $this->request->param();
        $setVal = serialize($param);
        Db::name('configure')->where('s_type',1)->update(['s_value'=>$setVal,'s_auth_id'=>$this->store['user']['store_user_id']]);
        return $this->renderSuccess('更新成功');
    }

    /**
     * 数据库
     */
    public function sql(){
        if (!$this->request->isAjax()) {
            $list = [];
            $table = input('table/s') ?? getenv('database.PREFIX').'admin';
            if ($table == 'admin') {
                // 返回所有的表
                $tables = Db::query("SHOW TABLE STATUS");
                foreach ($tables as $key => $value) {
                    $list[$key]['id'] = $value['Name'];
                    $list[$key]['name'] = $value['Name'];
                    $list[$key]['comment'] = $value['Comment'];
                    $list[$key]['data_length'] = 22;
                }
            } else {
                // 查询字段。
                $schema = Db::query('SHOW FULL COLUMNS FROM `'.$table.'`');
                if (!empty($schema)) {
                    foreach ($schema as $key => $value) {
                        $list[$key]['field'] = $value['Field'];
                        $list[$key]['default'] = $value['Default'];
                        $list[$key]['type'] = $value['Type'];
                        $list[$key]['collation'] = $value['Collation'];
                        $list[$key]['null'] = $value['Null'];
                        $list[$key]['comment'] = $value['Comment'];
                    }
                }
            }
           $this->assign('tables',$tables);
           return $this->fetch();
        }
    }

    /**
     * 数据库操作
     */
    public function operation($name = ''){
        if ($this->request->isAjax()){
            if (empty($name)) {
                return $this->error('请选择需要优化的数据库表！');
            }
            if(request()->param('type') == 'xiufu' && Db::query("REPAIR TABLE `{$name}`") ){
                return $this->success('数据表修复完成！');
            }
            if (request()->param('type') == 'youhua' && Db::query("OPTIMIZE TABLE `{$name}`")) {
                return $this->success('数据表优化完成！');
            }
        }
        $this->error('操作失败');
    }

    /**
     * 删除用户
     * @param $user_id
     * @return array
     * @throws \think\exception\DbException
     */
    public function delete($user_id)
    {
        // 用户详情
        $model = UserModel::detail($user_id);
        if ($model->setDelete()) {
            return $this->renderSuccess('删除成功');
        }
        return $this->renderError($model->getError() ?: '删除失败');
    }

    /**
     * 用户充值
     * @param $user_id
     * @param int $source 充值类型
     * @return array|bool
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function recharge($user_id, $source)
    {
        // 用户详情
        $model = UserModel::detail($user_id);
        if ($model->recharge($this->store['user']['user_name'], $source, $this->postData('recharge'))) {
            return $this->renderSuccess('操作成功');
        }
        return $this->renderError($model->getError() ?: '操作失败');
    }

    /**
     * 修改会员等级
     * @param $user_id
     * @return array|bool
     * @throws \think\Exception
     * @throws \think\exception\DbException
     */
    public function grade($user_id)
    {
        // 用户详情
        $model = UserModel::detail($user_id);
        if ($model->updateGrade($this->postData('grade'))) {
            return $this->renderSuccess('操作成功');
        }
        return $this->renderError($model->getError() ?: '操作失败');
    }

}
